﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using MyApp.Infrastructure.Messages;
using Microsoft.Practices.Composite.Presentation.Events;
using System.ComponentModel;
using System.Linq;

namespace MyApp.Menu
{
    public class MenuViewModel
    {
        public MenuViewModel()
        {
            this.Groups = new ObservableCollection<MenuGroup>();
            if (DesignerProperties.IsInDesignTool)
                return;

            Messenger.Get<MenuOptionMessage>().Subscribe(this.HandleMenuOptionMessage, ThreadOption.UIThread, true);
        }

        private void HandleMenuOptionMessage(MenuOptionMessageArgs args)
        {
            var group = this.Groups.FirstOrDefault(g => g.Name == args.Group);
            if (group == null)
            {
                group = new MenuGroup { Name = args.Group };
                this.Groups.Add(group);
            }


            group.Commands.Add(new MenuCommand
            {
                Command = args.Command,
                ImageSource = args.ImageSourceUri == null ? null : new Uri(args.ImageSourceUri, UriKind.RelativeOrAbsolute),
                Text = args.Text
            });
        }

        public ObservableCollection<MenuGroup> Groups { get; set; }
    }
}
